# -*- coding: utf-8 -*-
# keywords_heat.py
# Created by Hardy on 24th, Feb
# Copyright 2017 杭州网川教育科技有限公司. All rights reserved.

from querier.esquerier import ElasticSearchQuerier
from utils.province_code import PROVINCE_CODE, VERIFIED_TYPE

PROVINCE_DECODE = {v: k for k, v in PROVINCE_CODE.items()}
VERIFIED_TYPE_DECODE = {v: k for k, v in VERIFIED_TYPE.items()}


class PopularUserQuerier(ElasticSearchQuerier):
    def __init__(self, weibo_commu_analysis_multi_query, weibo_kol_brand_info_query):
        super(PopularUserQuerier, self).__init__(None, None, None)
        self.weibo_commu_analysis_multi_query = weibo_commu_analysis_multi_query
        self.weibo_kol_brand_info_query = weibo_kol_brand_info_query

    def search(self, args):
        res = []
        communicate_data = self.weibo_commu_analysis_multi_query.search(args)['data']
        for word_data in communicate_data:
            keyword = word_data['keyword'][0]
            user_ids = word_data['res']['user_ids']
            user_docs = word_data['res']['user_docs']
            date = word_data['res']['date']
            all_count = word_data['res']['all_count']
            a = {'user_ids': user_ids}
            user_infos = self.weibo_kol_brand_info_query.search(a)['infos']
            fans_list = []
            gender_list = []
            user_name_list = []
            verified_type_list = []
            user_type_list = []
            head_imgs = []
            user_id_list = []
            for user in user_ids:
                for user_info in user_infos:
                    if user_info.get('user_id') == user:
                        user_id_list.append(user)
                        fans = user_info.get('fans', 0)
                        gender = self.gender_decode(user_info.get('gender'))
                        head_img = user_info.get('avatar', '')
                        user_name = user_info.get('user_name', '')
                        verified_type = self.verified_type_decode(user_info.get('verified_type'))
                        user_type = user_info.get('user_type', '未知')
                        fans_list.append(fans)
                        gender_list.append(gender)
                        user_name_list.append(user_name)
                        verified_type_list.append(verified_type)
                        user_type_list.append(user_type)
                        head_imgs.append(head_img)

            fans_dict = {}
            for fans in fans_list:
                if fans <= 100:
                    fans_dict['0-100'] = fans_dict.get('0-100', 0) + 1
                elif 100 < fans <= 1000:
                    fans_dict['100-1000'] = fans_dict.get('100-1000', 0) + 1
                elif 1000 < fans <= 10000:
                    fans_dict['1000-10000'] = fans_dict.get('1000-10000', 0) + 1
                elif 10000 < fans <= 100000:
                    fans_dict['1w-10w'] = fans_dict.get('1w-10w', 0) + 1
                elif 100000 < fans <= 1000000:
                    fans_dict['10w-100w'] = fans_dict.get('10w-100w', 0) + 1
                elif 1000000 < fans <= 10000000:
                    fans_dict['100w-1000w'] = fans_dict.get('100w-1000w', 0) + 1
                else:
                    fans_dict['1000w+'] = fans_dict.get('1000w+', 0) + 1
            gender_dict = self.to_dict(gender_list)
            verified_type_dict = self.to_dict(verified_type_list)
            user_type_dict = self.to_dict(user_type_list)

            kv = {
                "keyword": keyword,
                "date": date,
                'all_count': all_count,
                'user_ids': user_id_list,
                'user_docs': user_docs,
                'fans': fans_list,
                'verified_type': verified_type_list,
                'fans_dict': fans_dict,
                'gender_dict': gender_dict,
                'user_name': user_name_list,
                'verified_type_dict': verified_type_dict,
                'user_type_dict': user_type_dict,
                'head_imgs': head_imgs
            }
            res.append(kv)
        return {"data": res}

    def _build_query(self, args):
        pass

    def _build_result(self, es_result, param):
        pass

    @staticmethod
    def province_decode(province_code):
        return PROVINCE_DECODE.get(province_code, '未知')

    @staticmethod
    def to_dict(lis):
        le = len(lis)
        b = [1] * le
        d = {}
        for k, v in zip(lis, b):
            d[k] = d.get(k, 0) + v
        return d

    @staticmethod
    def gender_decode(gender):
        if str(gender).strip() == 'm':
            return '男'
        elif str(gender).strip() == 'f':
            return '女'
        else:
            return '未知'

    @staticmethod
    def verified_type_decode(v):
        return VERIFIED_TYPE_DECODE.get(v, '未知')
